package com.how2java.tmall.web;

import com.how2java.tmall.dao.DepositDAO;
import com.how2java.tmall.pojo.Deposit;
import com.how2java.tmall.pojo.HistoryEntity;
import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.pojo.User;
import com.how2java.tmall.service.DepositService;
import com.how2java.tmall.service.HistoryService;
import com.how2java.tmall.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.PipedReader;

@Slf4j
@Controller
public class DepositController {
    @Autowired
    DepositService depositService;

    @Autowired
    HistoryService historyService;
    @Autowired
    ProductService productService;

    @GetMapping("/foredeposite")
    public String list(@RequestParam(value = "pid") int pid,
                       @RequestParam(value = "price") float money, HttpSession session) throws Exception {
        if (session.getAttribute("user") == null) {
            return "fore/login";
        }
        User user = (User) session.getAttribute("user");
        int userId = user.getId();

        System.out.println(pid);
        log.info(pid + "");
        log.info(money + "");
        log.info(userId + "");
        Deposit deposit = new Deposit();
        //保证金为支付价格的五分之一
        float depositMoney = money / 5;
        deposit.setMoney(depositMoney);

        deposit.setGoodsId(pid);
        deposit.setUserId(userId);
        deposit.setStatus(0);//未支付
        depositService.add(deposit);

        HistoryEntity historyEntity = new HistoryEntity();
        historyEntity.setGoodsId(pid);
        historyEntity.setGoodsPrice(money);
        historyEntity.setUserId(userId);
        historyService.add(historyEntity);
        //出现新报价,更新goods表中的goods_now_price
        Product product1 = productService.get(pid);
        product1.setPromotePrice(money);
        log.info(money + "更新的价格========");
        productService.add(product1);

        return "redirect:/product";
    }


}

